package com.jams.goodox.activity.serve;

import android.view.KeyEvent;
import android.view.View;
import android.widget.ProgressBar;

import com.tencent.smtt.sdk.WebChromeClient;
import com.tencent.smtt.sdk.WebSettings;
import com.tencent.smtt.sdk.WebView;
import com.tencent.smtt.sdk.WebViewClient;
import com.jams.goodox.BaseActivity;
import com.jams.goodox.R;
import com.jams.goodox.SePref;
import com.jams.goodox.constant.AppConfig;
import com.jams.goodox.constant.H5UrlConstants;
import com.jams.goodox.entity.RentingBean;
import com.jams.goodox.utils.permission.PhoneUtils;
import com.jams.goodox.utils.StringUtils;
import com.jams.goodox.utils.StyleUtil;

import java.util.HashMap;

import butterknife.Bind;
import butterknife.OnClick;

/**
 * 官方租房详情页
 *
 * create by 顾佳佳 on 2017/05/12
 */
public class OfficialRentingDetailActivity extends BaseActivity {

    @Bind(R.id.wv_official_renting_detail) 
    WebView wvOfficialRentingDetail;
    @Bind(R.id.progressBar1)
    ProgressBar progressBar;

    public static final String RENTING = "RENTING";
    private RentingBean rentingBean;
    // REPLACE: 2017/7/17 需要替换的

    @Override
    protected int getLayoutId() {
        return R.layout.activity_official_renting_detail;
    }

    @Override
    protected void init() {
        rentingBean = (RentingBean) getIntent().getSerializableExtra(RENTING);
        if(rentingBean == null){
            return;
        }

        HashMap<String, String> urlParams = new HashMap<>();
        urlParams.put("token", SePref.getToken());
        urlParams.put("mobileType", AppConfig.MOBILE_TYPE);
        urlParams.put("id", rentingBean.getId());
//        String sign = SignBuilder.encryptToSHA(SignBuilder.getSign(urlParams));
//        urlParams.put("sign",sign);

        String url = StringUtils.getUrl(H5UrlConstants.RENT,urlParams);
        wvOfficialRentingDetail.loadUrl(url);
        WebSettings webSettings = wvOfficialRentingDetail.getSettings();
//        webSettings.setJavaScriptEnabled(true);

        wvOfficialRentingDetail.setWebViewClient(new WebViewClient() {
            @Override
            public boolean shouldOverrideUrlLoading(WebView view, String url) {
                view.loadUrl(url);
                return true;
            }
        });
        wvOfficialRentingDetail.setWebChromeClient(new WebChromeClient() {

            @Override
            public void onProgressChanged(WebView webView, int newProgress) {
                if (newProgress == 100) {
                    progressBar.setVisibility(View.GONE);
                } else {
                    if (View.GONE == progressBar.getVisibility()) {
                        progressBar.setVisibility(View.VISIBLE);
                    }
                    progressBar.setProgress(newProgress);//设置进度值
                }
                super.onProgressChanged(webView, newProgress);
            }

            @Override
            public void onReceivedTitle(WebView webView, String s) {
                super.onReceivedTitle(webView, s);
                StyleUtil.titleBackKey(OfficialRentingDetailActivity.this, s);
            }
        });
    }


    @OnClick(R.id.lv_renting_call)
    public void onViewClicked() {
        //请求电话权限
        if(rentingBean == null){
            return;
        }
       new PhoneUtils(this).makeCall(rentingBean.getTitle(),rentingBean.getTelephone(),true);
    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_BACK && wvOfficialRentingDetail.canGoBack()) {
            wvOfficialRentingDetail.goBack();// 返回前一个页面
            return true;
        } else {
            finish();
        }
        return super.onKeyDown(keyCode, event);
    }
}
